package com.example.gis;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolygonOptions;
import com.baidu.mapapi.map.PolylineOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;
import java.util.List;

public class MapClickActivity extends AppCompatActivity {
    private MapView mMapView = null;
    private BaiduMap mBaiduMap=null;
    private List<LatLng> points=new ArrayList<>();
    private Overlay mPolyline=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map_click);
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap=mMapView.getMap();
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                points.add(latLng);

                if(mPolyline!=null){
                    mPolyline.remove();
                }

                if(points.size()>2){
//                    //设置折线的属性
//                    OverlayOptions mOverlayOptions = new PolylineOptions()
//                            .width(10)
//                            .color(0xAAFF0000)
//                            .points(points);
                    PolygonOptions mPolygonOptions = new PolygonOptions()
                            .points(points)
                            .fillColor(0xAAFFFF00) //填充颜色
                            .stroke(new Stroke(5, 0xAA00FF00)); //边框宽度和颜色
                    //在地图上绘制折线
                    //mPloyline 折线对象
                    mPolyline = mBaiduMap.addOverlay(mPolygonOptions);
                }

            }

            @Override
            public void onMapPoiClick(MapPoi mapPoi) {

            }
        });



    }
}